using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace MacomberMap.Common.Events
{
    /// <summary>
    /// This class handles an event for information
    /// </summary>
    public class MM_Event_Information : MM_Event
    {
        #region Variable declarations
        /// <summary>The text of the information</summary>
        public String Text;

        /// <summary>The source of the event</summary>
        public Object Source;
        #endregion

        /// <summary>
        /// Initialize a new informational point
        /// </summary>
        /// <param name="Text">The informational text</param>
        /// <param name="Source">The source of the text</param>
        public MM_Event_Information(String Text, Object Source)
        {
            this.Text = Text;
            this.Source = Source;
        }

        /// <summary>
        /// Compare two event information points
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(object obj)
        {            
            return Text.CompareTo((obj as MM_Event_Information).Text);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="OutMenu"></param>
        public override void AddMenuItems(System.Windows.Forms.ContextMenuStrip OutMenu)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ListViewItem BuildListViewItem()
        {
            ListViewItem NewItem = new ListViewItem(this.Text);
            NewItem.SubItems.Add(this.EventState.ToString());
            NewItem.SubItems.Add(TimeText);
            NewItem.ImageIndex = EventType.Index;
            return NewItem;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="OutParent"></param>
        public override void DisplayDetails(System.Windows.Forms.Control OutParent)
        {
            
            if (Source != null)
            {                
                TreeView tvOut = new TreeView();
                tvOut.Dock = DockStyle.Fill;
                this.AddNodeItem("Information", this.Text, tvOut.Nodes);
                this.AddNodeItem("Source", Source.GetType().Name, tvOut.Nodes);                
                TreeNode tnFields = tvOut.Nodes.Add("Fields");
                foreach (FieldInfo fI in Source.GetType().GetFields())                    
                    {
                        TreeNode NewProp = tnFields.Nodes.Add(fI.Name);
                        NewProp.Nodes.Add("Type: " + fI.FieldType.Name);
                        AddNodeItem("Value", fI.GetValue(Source), NewProp.Nodes);                        
                        NewProp.EnsureVisible();
                    }                            

                TreeNode tnProperties = tvOut.Nodes.Add("Properties");
                foreach (PropertyInfo pI in Source.GetType().GetProperties())                    
                {
                    TreeNode NewProp = tnProperties.Nodes.Add(pI.Name);
                    NewProp.Nodes.Add("Type: " + pI.PropertyType.Name);
                    if (pI.PropertyType.IsGenericParameter)
                        AddNodeItem("Value", pI.GetValue(Source, null), NewProp.Nodes);
                    
                    NewProp.EnsureVisible();
                }
                tvOut.Nodes[0].EnsureVisible();
                OutParent.Controls.Add(tvOut);
            }                                       
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="lvItem"></param>
        public override void UpdateListViewItem(ListViewItem lvItem)
        {
            lvItem.SubItems[0].Text = this.Text;
            lvItem.SubItems[1].Text = this.EventState.ToString();
            lvItem.SubItems[2].Text = TimeText;
        }
    }
}
